<?php
namespace Ng\Controller;
use Think\Controller;
class QuestionController extends Controller {
  /**
  * 查询用户信息分页数据
  * @param integer|string $currentPage 分页页码
  * @param integer|string $pageSize 分页当前页显示的条数
  * @param string $status 状态码,根据状态码筛选
  * @return void 没有返回值
  * @method GET
  */
  public function questionList($status='',$currentPage=1,$pageSize=10){
    \Think\Log::record('查询问题分页','INFO');
    if (IS_GET) {
      if ($status == '') {
        $where = '1';
      } else {
        $where = 'think_question.question_status='.$status;
      }

      $model = D('question');
      $res = $model->getlist($where,$currentPage,$pageSize);

      if ($res) {
        $data->code = 200;
        $data->msg = '查询成功';
        $data->obj = $res->obj;
        $data->total = $res->total;
        $this->ajaxReturn($data);
      } else {
        $data->code = 201;
        $data->msg = '查询失败';
        $this->ajaxReturn($data);
      }
    } else {
      $data['code'] = 400;
      $data['msg'] = '提交方式有误';
      $this->ajaxReturn($data);
    }
  }

  public function getUserPublishList($page=10){
    \Think\Log::record('用户评论排行榜','INFO');
    if (IS_GET) {
      $model = D('question');
      $res = $model->getUserPublishList($page);

      if ($res) {
        $data->code = 200;
        $data->msg = '查询成功';
        $data->obj = $res->obj;
        $this->ajaxReturn($data);
      } else {
        $data->code = 201;
        $data->msg = '查询失败';
        $this->ajaxReturn($data);
      }
    } else {
      $data['code'] = 400;
      $data['msg'] = '提交方式有误';
      $this->ajaxReturn($data);
    }
  }

  public function getQuestionWatchList($page=12){
    \Think\Log::record('问题查看排行榜','INFO');
    if (IS_GET) {
      $model = D('question');
      $res = $model->getQuestionWatchList($page);

      if ($res) {
        $data->code = 200;
        $data->msg = '查询成功';
        $data->obj = $res->obj;
        $this->ajaxReturn($data);
      } else {
        $data->code = 201;
        $data->msg = '查询失败';
        $this->ajaxReturn($data);
      }
    } else {
      $data['code'] = 400;
      $data['msg'] = '提交方式有误';
      $this->ajaxReturn($data);
    }
  }

  public function getQuestionPublishList($page=12){
    \Think\Log::record('用户发表排行榜','INFO');
    if (IS_GET) {
      $model = D('question');
      $res = $model->getQuestionPublishList($page);

      if ($res) {
        $data->code = 200;
        $data->msg = '查询成功';
        $data->obj = $res->obj;
        $this->ajaxReturn($data);
      } else {
        $data->code = 201;
        $data->msg = '查询失败';
        $this->ajaxReturn($data);
      }
    } else {
      $data['code'] = 400;
      $data['msg'] = '提交方式有误';
      $this->ajaxReturn($data);
    }
  }

  public function getQuestionListByUserId($id='',$currentPage=1,$pageSize=10){
    \Think\Log::record('根据用户Id查询用户发表帖子','INFO');
    if (IS_GET) {
      $model = M('question');
      if (!$id || $id=='') {
        $data->code = 201;
        $data->msg = '没有传入有效的参数';
        $this->ajaxReturn($data);
      }

      $where = 'q.question_uid="'.$id.'"';
      $res = $model
      ->alias('q')
      ->field('q.id AS qid,q.question_title,q.question_time,q.question_view,q.question_status,count(q.id) AS count')
      ->join('think_answer as a on q.id=a.answer_qid','left')
      ->where($where)
      ->group('qid')
      ->order('q.question_time desc')
      ->page($currentPage,$pageSize)
      ->select();

      $total = $model
      ->alias('q')
      ->where($where)
      ->count();

      if ($res) {
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['obj'] = $res;
        $data['total']=$total;
        $this->ajaxReturn($data);
      } else {
        $data['code'] = 201;
        $data['msg'] = '未查询到对应数据';
        $this->ajaxReturn($data);
      }
    } else {
      $data->code = 400;
      $data->msg = '提交方式有误';
      $this->ajaxReturn($data);
    }
  }

  public function getAnswerListByUserId($id='',$currentPage=1,$pageSize=10){
    \Think\Log::record('根据用户Id查询用户发表回答','INFO');
    if (IS_GET) {
      $model = M('answer');
      if (!$id || $id=='') {
        $data->code = 201;
        $data->msg = '没有传入有效的参数';
        $this->ajaxReturn($data);
      }

      $where = 'a.answe_uid="'.$id.'"';
      $res = $model
      ->alias('a')
      ->field('a.id,q.id AS qid,a.answer_time AS createTime,q.question_title,a.answer_content')
      ->join('think_question AS q ON q.id = a.answer_qid','left')
      ->where($where)
      ->order('a.answer_time desc')
      ->page($currentPage,$pageSize)
      ->select();

      $total = $model
      ->alias('a')
      ->where($where)
      ->count();

      if ($res) {
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['obj'] = $res;
        $data['total']=$total;
        $this->ajaxReturn($data);
      } else {
        $data['code'] = 201;
        $data['msg'] = '未查询到对应数据';
        $this->ajaxReturn($data);
      }
    } else {
      $data->code = 400;
      $data->msg = '提交方式有误';
      $this->ajaxReturn($data);
    }
  }
}
